<?php

class Controller_Edit extends FLEA_Controller_Action
{
    function actionindex(){
        if($this->_isPOST()){
            $word= $_POST['word'];
            $type = $_POST['type'];
            switch($type){
            case 'pic':
                redirect(url('pic','listbyword',array('word'=>$word)));
                break;
            case 'sentence':
                redirect(url('sentence','listbyword',array('word'=>$word)));
                break;
            }
        }
        else{
            $smarty =& $this->_getView();
            $smarty->display('search_admin.html');
        }
    }
   function actioneditvoice(){
        if($this->_isPOST()){
            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            if ($uploader->isFileExist('voice')) {
                $file =& $uploader->getFile('voice');
            }
            $row['id'] = (int)$_POST['id'];
            $row['Word'] = $_POST['word'];
            $row['No'] = (int)$_POST['no'];
            $row['Type'] = (int)$_POST['type'];
            $uploader =& FLEA::getSingleton('FLEA_Helper_FileUploader');
            if ($uploader->isFileExist('voice')) {
                $file =& $uploader->getFile('voice');
            }
            else{
                js_alert('文件不存在', 'history.back();');
            }
            if (!$file->check(FLEA::getAppInf('voiceFileExts'))) {
                js_alert('不允许的文件类型', 'history.back();');
            }
            $row['Type'] = $file->getExt();
            $filepath = $file->isMoved()?$file->getNewPath():$file->getTmpName();
            $row['VoiceData'] = file_get_contents($filepath);
            $table_pic =& FLEA::getSingleton('Table_tofowordvoice');
            if($table_pic->save($row)){
                js_alert('更新成功','history.back();');
            }
            else{
                js_alert('更新失败，请重试或联系管理员','history.back();');
            }
        }
        else{
            $id = $_GET['id'];
            if(empty($id)){
                echo 'please input the word';
            }
            $smarty =& $this->_getView();
            $table_sentence =& FLEA::getSingleton('Table_tofowordvoice');
            $sentence = $table_sentence->find((int)$id);
            unset($sentence['Voice_Data']);
            $smarty->assign('data', $sentence);
            $smarty->display('editvoice.html');
        }
    }
    function actionshowbysize(){
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $smarty =& $this->_getView();
        $words = $table_words->find((int)$_GET['id']);
        $image = imagecreatefromstring($words[PicBuf]);
        FLEA::loadFile('APP_Helper_MyImage');
        $out = new resizeimage($image,150,100,0);
        $image = $out->resize();
        Header("Content-type:image/png"); 
        imagepng($image);
    }
    function actionaudio(){
        $table_voice = & FLEA::getSingleton('Table_tofowordvoice');
        $voice = $table_voice->find((int)$_GET['id']);
        Header("Content-type:audio/wav"); 
        print $voice[VoiceData];
    }
   function actiontest(){
	    $smarty =& $this->_getView();
        $table_words = & FLEA::getSingleton('Table_tofowordpic');
        $words = $table_words->find(23);
        $words['PicBuf'] = null;
        dump($words);
        $table_words = & FLEA::getSingleton('Table_tofowordvoice');
        $words = $table_words->find(23);
        $words['VoiceData'] = null;
        dump($words);
        $table_words = & FLEA::getSingleton('Table_wordsentence');
        $words = $table_words->find(23);
        dump($words);
        $table_words = & FLEA::getSingleton('Table_tofoword');
        $words = $table_words->find(23);
        dump($words);
        $table_words = & FLEA::getSingleton('Table_word2yinbiao');
        $words = $table_words->find(188);
        $words[HanYuJieshe] = iconv("GB2312","UTF-8",$words[HanYuJieshe]);
        dump($words);
    }
}
